package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.ShopCarDao;
import com.baichang.taoshunda.entity.ShopCar;
import com.baichang.taoshunda.service.GoodsService;
import com.baichang.taoshunda.service.ShopcarService;
import net.sf.json.JSONArray;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 17/08/23.
 */
@Service
public class ShopcarServiceImpl implements ShopcarService {
    @Autowired
    private ShopCarDao shopCarDao;
    @Autowired
    private GoodsService goodsService;

    /**
     * 添加购物车
     *
     * @param userId             用户编号
     * @param bussId             商家编号
     * @param goodsId            商品编号
     * @param number             数量
     * @param goodsSpec          规格
     * @param operator
     * @param isBespeak
     * @param isNoReasonReturn
     * @param isHomeInstallation
     * @param isFree
     * @param unit               @return
     * @param goodsPrice
     */
    @Override
    public boolean addShopCar(Integer userId, Integer bussId, Integer goodsId, Integer number, String goodsSpec, Integer operator, Integer isBespeak, Integer isNoReasonReturn, Integer isHomeInstallation, Integer isFree, String unit, Double goodsPrice) throws ServiceException {
        if (userId == 0) {
            throw new ServiceException("用户不存在");
        }
        String spec1 = goodsSpec.replaceAll("/", "");
        String spec2 = StringEscapeUtils.unescapeJava(spec1);
        goodsSpec = spec2.replaceAll("\"\\{", "\\{").replaceAll("\\}\"", "\\}");
        //根据用户编号与商品编号获取用户的购物车信息
        List<ShopCar> shopCars = shopCarDao.findCarByGoodsAndUserId(userId, goodsId);
        if (shopCars != null && shopCars.size() > 0) {
            for (ShopCar shopCar : shopCars) {
                String oldSpec = shopCar.getGoodsSpec();
                JSONArray oldJsonArray = JSONArray.fromObject(oldSpec);
                JSONArray newJsonArray = JSONArray.fromObject(goodsSpec);
                if (oldJsonArray.compareTo(newJsonArray) == 0) {
                    //说明该规格的商品已经在购物车中了
                    Integer goodsNum;
                    if (operator == 1) {
                        if (number != 0) {
                            goodsNum = (shopCar.getNumber() == null ? 0 : shopCar.getNumber()) + number;
                        } else {
                            goodsNum = (shopCar.getNumber() == null ? 0 : shopCar.getNumber()) + 1;
                        }

                    } else {
                        goodsNum = (shopCar.getNumber() == null ? 0 : shopCar.getNumber()) - 1;
                    }
                    if (goodsNum > 0) {
                        return shopCarDao.updateCarNumber(shopCar.getId(), goodsNum) > 0;
                    } else {
                        return shopCarDao.deleteById(shopCar.getId()) > 0;
                    }
                }
            }
        }
        ShopCar shopCar = new ShopCar();
        shopCar.setUserId(userId);
        shopCar.setBussId(bussId);
        shopCar.setGoodsSpec(goodsSpec);
        shopCar.setGoodsId(goodsId);
        if (number != 0) {
            shopCar.setNumber(number);
        } else {
            shopCar.setNumber(1);
        }
        shopCar.setIsBespeak(isBespeak);
        shopCar.setIsHomeInstallation(isHomeInstallation);
        shopCar.setIsFree(isFree);
        shopCar.setIsNoReasonReturn(isNoReasonReturn);
        shopCar.setUnit(unit);
        shopCar.setGoodsPrice(BigDecimal.valueOf(goodsPrice));
        return shopCarDao.save(shopCar) > 0;

    }

    /**
     * 修改购物车
     *
     * @param carId     购物车编号
     * @param userId    用户编号
     * @param bussId    商家编号
     * @param goodsId   商品编号
     * @param number    数量
     * @param goodsSpec 规格
     * @return
     */
    @Override
    public boolean updateShopCar(Integer carId, Integer userId, Integer bussId, Integer goodsId, Integer number, String goodsSpec) {
        String spec1 = goodsSpec.replaceAll("/", "");
        String spec2 = StringEscapeUtils.unescapeJava(spec1);
        goodsSpec = spec2.replaceAll("\"\\{", "\\{").replaceAll("\\}\"", "\\}");
        if (number == 0) {
            return shopCarDao.deleteById(carId) > 0;
        } else {
            return shopCarDao.updateShopCar(carId, userId, bussId, goodsId, number, goodsSpec) > 0;
        }

    }

    /**
     * 删除购物车
     *
     * @param userId 用户编号
     * @param bussId 商家编号
     * @return
     */
    @Override
    public boolean deleteShopCarById(Integer userId, Integer bussId) {

        return shopCarDao.deleteShopCarByBussId(userId, bussId);
    }

    /**
     * 查询用户在商家的购物车
     *
     * @param userId 用户编号
     * @param bussId 商家编号
     * @return
     */
    @Override
    public Map<String, Object> findShopCar(Integer userId, Integer bussId) {
        Map<String, Object> map = new HashedMap();
        map = shopCarDao.getAllNumberAndAllMoney(userId, bussId);
        map.put("list", shopCarDao.findShopCar(userId, bussId));
        return map;
    }

    @Override
    public boolean removeAllCar(Integer userId) {
        return shopCarDao.removeAllCar(userId);
    }


}
